package com.logis.ryanrqwork.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.logis.ryanrqwork.MainActivity;
import com.logis.ryanrqwork.R;
import com.logis.ryanrqwork.UserInfo;
import com.logis.ryanrqwork.dao.User;
import com.logis.ryanrqwork.daoutil.UserDaoController;

public class LoginActivity extends AppCompatActivity {

    private EditText edittext_username;
    private EditText edittext_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        edittext_username = ((EditText) findViewById(R.id.edittext_username));
        edittext_password = ((EditText) findViewById(R.id.edittext_password));
        findViewById(R.id.textview_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });
        findViewById(R.id.button_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName=edittext_username.getText().toString();
                String password=edittext_password.getText().toString();
                User user= UserDaoController.getUserInfo(LoginActivity.this,userName,password);
                if(user==null){
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                UserInfo.user=user;
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
            }
        });
    }
}
